package com.fangrui.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

//Created by ✎﹏ℳ๓独赏☆独醉 on 2025/4/27 11:06
//@WebFilter(urlPatterns = "/*")
public class DemoFilter implements Filter {
    @Override//初始化方法，只会在项目启动时执行一次
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init 初始化方法执行了");
//        Filter.super.init(filterConfig);
    }

    @Override//拦截方法，每次拦截到请求都会执行
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("拦截到了请求");
        long start = System.currentTimeMillis();
        filterChain.doFilter(servletRequest, servletResponse);
        long end = System.currentTimeMillis();
        System.out.println("接口响应耗时" + (end - start) + "ms");
    }

    @Override//销毁方法，只会在服务器关闭时执行
    public void destroy() {
        System.out.println("destroy 销毁方法执行了");
//        Filter.super.destroy();
    }
}
